package com.example.fbztest_vue.controller;

import com.example.fbztest_vue.annotiation.UserLoginToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/api/file")
public class FileController {

    @Autowired
    HttpServletRequest request;

    @PostMapping("getImage")
    @UserLoginToken
    public HashMap<String,Object> upload(@RequestParam("image") MultipartFile file) throws IOException {
        log.info("请求从UserId:{}中来",request.getHeader("fbzUserId"));
        String userID=request.getHeader("fbzUserId");
        HashMap<String,Object> map=new HashMap<>();
        log.info("当前文件情况:{}",file.getOriginalFilename());
        File newPath=new File("d://headers//"+userID+"//"+file.getOriginalFilename());
        if (!newPath.exists()){
            newPath.mkdirs();
        }
        file.transferTo(newPath);
        return map;
    }

//    @PostMapping("getVideo")
//    public HashMap<String,Object> upload2(@RequestParam("video") MultipartFile file) throws IOException {
//        HashMap<String,Object> map=new HashMap<>();
//        log.info("当前文件名称:{}",file.getOriginalFilename());
//        file.transferTo(new File("d://video//"+file.getOriginalFilename()));
//        return map;
//    }
}

